package com.oa.ylms.apps.business.limit.impl;

import java.util.List;

import com.oa.ylms.apps.business.limit.ILimitInfoService;
import com.oa.ylms.apps.business.limit.IPostLimitService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.limit.ILimitInfoDao;
import com.oa.ylms.apps.entity.limit.LimitInfoValue;

public class LimitInfoServiceImpl implements ILimitInfoService{
	
	private ILimitInfoDao infoDao;
	private LimitInfoValue limitInfo;
	private IPostLimitService postLimitService;
	
	// Service method

	@Override
	public int addLimitInfo(String limitName, int parentId, String limitUrl)
			throws Exception {
		limitInfo.setLimitName(limitName);
		limitInfo.setParentId(parentId);
		limitInfo.setLimitUrl(limitUrl);
		limitInfo.setStates(IDEDStates.ENABLE);
		return infoDao.addLimitInfo(limitInfo);
	}
	
	@Override
	public int countTpages(int rows) throws Exception {
		return infoDao.countTPages(rows);
	}
	
	@Override
	public String deleteLimitInfo(String limitIds) throws Exception {
		String unDelIds = "";
		String[] idArr = limitIds.split(",");
		for (String idStr : idArr){
			int limitId = Integer.parseInt(idStr);
			limitInfo = queryByLimitId(limitId);
			limitInfo.setStates(IDEDStates.DELETE);
			boolean result = infoDao.editLimitInfo(limitInfo);
			if(result == false){
				unDelIds = limitId +",";
				continue;
			}
		}
		return unDelIds;
	}
	
	@Override
	public boolean modifyLimitInfo(int limitId, String limitName, int parentId,
			String limitUrl) throws Exception {
		limitInfo = queryByLimitId(limitId);
		if(limitInfo != null){
			limitInfo.setLimitName(limitInfo.getLimitName());
			limitInfo.setLimitUrl(limitInfo.getLimitUrl());
			limitInfo.setParentId(limitInfo.getParentId());
			return infoDao.editLimitInfo(limitInfo);
		}
		return false;
	}
	
	@Override
	public LimitInfoValue queryByLimitId(int limitId) throws Exception {
		limitInfo.setLimitId(limitId);
		return infoDao.queryByLimitId(limitInfo);
	}
	
	@Override
	public List<LimitInfoValue> queryByLimitIds(String limitIds) throws Exception {
		if(limitIds != null){ 
			return infoDao.queryByLimitIds(limitIds);
		}
		return null;
	}
	
	@Override
	public List<LimitInfoValue> queryByParentId(String parentId, Integer postId)
			throws Exception {
		int parentId_INT = Integer.parseInt(parentId);
		limitInfo.setParentId(parentId_INT);
		return infoDao.queryByParentId(limitInfo,postLimitService.queryByPostId(postId));
	}
	
	@Override
	public List<LimitInfoValue> queryLimitInfoWithPage(int rows, int pageNo)
			throws Exception {
		return infoDao.queryLimitInfoWithPage(rows, pageNo);
	}
	
	@Override
	public boolean toDisable(int limitId) throws Exception {
		limitInfo = queryByLimitId(limitId);
		if(limitInfo != null){
			limitInfo.setStates(IDEDStates.DISABLE);
			return infoDao.editLimitInfo(limitInfo);
		}
		return false;
	}
	
	@Override
	public boolean toEnable(int limitId) throws Exception {
		limitInfo = queryByLimitId(limitId);
		if(limitInfo != null){
			limitInfo.setStates(IDEDStates.ENABLE);
			return infoDao.editLimitInfo(limitInfo);
		}
		return false;
	}
	
	// Service property accessors
	
	public ILimitInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(ILimitInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public LimitInfoValue getLimitInfo() {
		return limitInfo;
	}

	public void setLimitInfo(LimitInfoValue limitInfo) {
		this.limitInfo = limitInfo;
	}

	public void setPostLimitService(IPostLimitService postLimitService) {
		this.postLimitService = postLimitService;
	}

	public IPostLimitService getPostLimitService() {
		return postLimitService;
	}
	
}
